package xyz.xiangyushui.controller.membership.renewal;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xyz.xiangyushui.dao.user.member.MembershipDao;
import xyz.xiangyushui.dao.user.member.RenewalHistoryDao;

/**
 * Servlet implementation class RenewalPost
 */
@WebServlet("/member/renewalPost")
public class RenewalPost extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MembershipDao md;
	private RenewalHistoryDao rhd;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RenewalPost() {
        super();
        md=new MembershipDao();
        rhd=new RenewalHistoryDao();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		double value=0;
		try{
			value=Double.parseDouble(request.getParameter("money"));
		}catch(NumberFormatException e){
			response.sendRedirect(request.getContextPath()+"/member/renewal");
		}
		
		
		if(value>0){
			String userName=(String) request.getSession(false).getAttribute("userName");
			if(value>=300&&value<600){
				md.updateRatings(1,userName);
			}else if(value>=600&&value<900){
				md.updateRatings(2, userName);
			}else if(value>=900){
				md.updateRatings(3, userName);
			}else{
				
			}
			md.changeBalance(userName, value);
			rhd.addRenewalRecord(userName, value);
		}
		response.sendRedirect(request.getContextPath()+"/member/myMembership");
	}

}
